#include "rtc.h"
#include "cmsis_os.h"
#include "bsp_rtc.h"


extern RTC_HandleTypeDef hrtc;
extern osSemaphoreId_t sensorSemHandle;  // 任务用的信号量



void BSP_RTC_Init(void) {

    MX_RTC_Init();
}

/**
  * @brief  RTC AlarmA 中断回调函数
  * @param  hrtc: RTC handle
  */
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{

    // 设置下一次闹钟（下一整点）
    RTC_TimeTypeDef sTime;
    RTC_AlarmTypeDef sAlarm;

    // 读取当前时间
    HAL_RTC_GetTime(hrtc, &sTime, RTC_FORMAT_BCD);

    // 下一整点 = 当前小时 + 1，分钟秒清零
    uint8_t nextHour = ((sTime.Hours + 1) % 24) & 0xFF; // 24小时制，溢出回0

    sAlarm.AlarmTime.Hours   = nextHour;
    sAlarm.AlarmTime.Minutes = 0x00;
    sAlarm.AlarmTime.Seconds = 0x00;
    sAlarm.AlarmMask         = RTC_ALARMMASK_DATEWEEKDAY; // 屏蔽日期比较
    sAlarm.AlarmSubSecondMask= RTC_ALARMSUBSECONDMASK_ALL;
    sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
    sAlarm.AlarmDateWeekDay  = 0x01;
    sAlarm.Alarm = RTC_ALARM_A;

    HAL_RTC_SetAlarm_IT(hrtc, &sAlarm, RTC_FORMAT_BCD);

    // 给采集任务发信号
    osSemaphoreRelease(sensorSemHandle);
}

